var appInstance = getApp();
var pageData    = {
  data: {
    free_vessel4:{
      li:[{"img":"http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg",'title':'exlm','content':'neirong','time':'2010/12/1',"eventParams":"aa","eventHandler":"tapInnerLinkHandler"},{"img":"http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg",'title':'exlm','content':'neirong','time':'2010/12/1',"eventParams":"aa","eventHandler":"tapInnerLinkHandler"}]
    },
    shade:true, 
    picker:{'li':[{'img':'../../images/shelve.png','txt':'新增','eventParams':'shelve',"eventHandler":"picker"},{'img':'../../images/off_shelve.png','txt':'修改','eventParams':'off_shelve',"eventHandler":"picker"},{'img':'../../images/delete.png','txt':'删除','eventParams':'delete',"eventHandler":"picker"},]}, 
  },
  app_title: '石雕小程序',
  app_description: '加油!',
  page_router: 'page10010',
  page_router1: '10010',
  onLoad: function (e) {
    var that =this;
    this.searchPrdList();
     },
  onShareAppMessage: function(){
    var pageRouter = this.page_router;
    var pageRouter1 = this.page_router1;
    return {
      title: this.app_title || '产品设计',
      desc: this.app_description || '',
      path: '/pages/'+pageRouter+'/'+pageRouter1
    }
  },
  onShow: function(){ 
  },
  navbarTap: function(e){
    var id = e.currentTarget.dataset.idx;
    var navbar = this.data.navbar;
    this.setData({  
      currentTab: id  
    })
  },
  searchPrdList:function(){
    var that = this;
    var pageNo = this.data.free_vessel4.pageNo;
    var param = appInstance.globalData.session_key;
    var searchPrdList ={
      token :param.myXcxSessionId,
      source:'1',  
      userId:param.userId,  
    }
    appInstance.sendRequest({
      url:'/cust/getMyFavoriteList',
      data:searchPrdList,
      method:'POST',
      success: function(res){
        var data = res.userdata
        console.log(data);
        var arr = [];
        // for(var i=0,len=data.length;i<len;i++){
        //   var dataset = {}
        //   dataset.title=data[i].link;
        //   dataset.img=data[i].imgPath;
        //   dataset.content=data[i].custPhone;
        //   dataset.time = data[i].collectDate;
        //   dataset.eventParams=data[i].favoriteType;
        //   dataset.eventHandler='tapInnerLinkHandler';
        //   arr.push(dataset);
        // }                
        // that.setData({
        //    free_vessel4:{"li":arr} 
        // })
      },
      fail: function(res){
        console.log('getmode fail');
      },
      complete: function(res){
      }
    }) 
  },
  picker:function(e){
    let that = this;     
    var tapeven = e.currentTarget.dataset.eventParams;
    console.log(tapeven);
    var prods=[];
    var content = that.data.free_vessel4.content;
    for(var i=0,len=content.length;i<len-1;i++){
      var _content = content[i];
      if(_content.status==true){
        prods.push({'productId':_content.eventParams[0]})
      }
    }
    var param = appInstance.globalData.session_key;
    var prod_param = {
        prods:prods,
        source:'1',
        token:param.myXcxSessionId
        }
    console.log(prod_param)
    that.setData({
        shade:true
      })
    if(tapeven=='delete'){
       appInstance.sendRequest({
              url:'/prod/delProds',
              data:prod_param,
              method:'POST', 
              header:{
              'content-type':'application/json'
              },
              success: function(res){
                if(res.result == 0){  
                  that.searchPrdList();                     
                  console.log(res.msg)
                }else{
                  console.log(res.msg)
                  //提示无数据并navigateback
                }
              },
              fail: function(res){
                console.log('getmode fail');
              },
              complete: function(res){
              }
       },'https://bc.zhaojx.net/bizcat/webservice/rest') 
    }else if(tapeven=='shelve'){
      appInstance.sendRequest({
              url:'/prod/putOnProd',
              data:prod_param,
              method:'POST',
              header:{
              'content-type':'application/json'
              },
              success: function(res){
                if(res.result == 0){  
                  that.searchPrdList();                       
                  console.log(res.msg)
                }else{
                  console.log(res.msg)
                  //提示无数据并navigateback
                }
              },
              fail: function(res){
                console.log('getmode fail');
              },
              complete: function(res){
              }
       },'https://bc.zhaojx.net/bizcat/webservice/rest') 
    }else if(tapeven=='off_shelve'){
      appInstance.sendRequest({
              url:'/prod/pullOffProd',
              data:prod_param,
              method:'POST', 
              header:{
              'content-type':'application/json'
              },
              success: function(res){
                if(res.result == 0){  
                  that.searchPrdList();                      
                  console.log(res.msg)
                }else{
                  console.log(res.msg)
                  //提示无数据并navigateback
                }
              },
              fail: function(res){
                console.log('getmode fail');
              },
              complete: function(res){
              }
       },'https://bc.zhaojx.net/bizcat/webservice/rest') 
    }
  },
  content_call:function(e){
    console.log(e)
    return appInstance.makePhoneCall(e)
  },
  pick_show:function(){
    var that =this;
    var boolean_set = !that.data.shade;
    console.log(boolean_set);
    //make it hide
     if(boolean_set==true){
      var arr = that.data.free_vessel4.content;
      for(var i=0,len=arr.length;i<len-1;i++){        
          arr[i].status=false;
      };
      var pageNo = this.data.free_vessel4.pageNo;
      that.setData({
        free_vessel4:{"content":arr,'pageNo':pageNo}, 
        shade:boolean_set
       })
     }else{
      that.setData({
        shade:boolean_set
      })
     }
  },
  chooseAll:function(){
    var that =this;
    var boolean_set = !that.data.shade;
    console.log(boolean_set);
    var arr = that.data.free_vessel4.content;
     //make it hide
     var status = arr[0].status
     if(boolean_set==true){        
        for(var i=0,len=arr.length;i<len-1;i++){
          //逻辑需要更细        
            arr[i].status=!status;
        };
        var pageNo = this.data.free_vessel4.pageNo;
        that.setData({
          free_vessel4:{"content":arr,'pageNo':pageNo,}, 
        })
     }else{
        for(var i=0,len=arr.length;i<len-1;i++){        
            arr[i].status=true;
        };
        var pageNo = this.data.free_vessel4.pageNo;
        that.setData({
          free_vessel4:{"content":arr,'pageNo':pageNo}, 
          shade:boolean_set
        })
     }
  },
  //长按待选项
  tapInnerLinkHandler:function(e){
    let that = this;     
    var tapeven = e.currentTarget.dataset.eventParams;
    console.log(tapeven)
    if(that.data.shade == false){
      var arr = that.data.free_vessel4.content;
      for(var i=0,len=arr.length;i<len-1;i++){
        if(arr[i].eventParams == tapeven){         
            arr[i].status=!arr[i].status;
        }
      };
      var pageNo = this.data.free_vessel4.pageNo;
      that.setData({
        free_vessel4:{"content":arr,'pageNo':pageNo}, 
      })
    }else{
      if(tapeven=='add'){
          wx.navigateTo({
            url: '../addproduct/addproduct',
            success: function(res){
              // success
            },
          })
        }else{
          var param = appInstance.globalData.session_key;
          var userId = param.userId;
          wx.navigateTo({
              url: '../companydetail/companydetail?userId='+userId+'&companyId='+tapeven,
            success: function(res){
              // success
          },
        })
      }    
      // var touchTime = that.data.touch_end - that.data.touch_start;  
      // console.log(touchTime);  
      // if (touchTime > 350) {}  
   }
  },
   //按下事件开始  
 mytouchstart: function (e) {  
   let that = this;  
   that.setData({  
     touch_start: e.timeStamp  
   })  
   console.log(e.timeStamp + '- touch-start')  
 },  
 //按下事件结束  
 mytouchend: function (e) {  
   let that = this;  
   that.setData({  
     touch_end: e.timeStamp  
   })  
   console.log(e.timeStamp + '- touch-end')  
 },  
  
};
Page(pageData);